package com.tuanzi.loan.business.repository.family.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanProductMatchRule.LOAN_PRODUCT_MATCH_RULE;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.tuanzi.loan.business.repository.family.custom.ProductMatchRuleRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.family.ProductMatchRuleSearchRequest;
import com.tuanzi.loan.vo.response.family.ProductMatchRuleSearchResponse;

/**
 * @author Ray 2017/09/21
 */
@SuppressWarnings({ "rawtypes" })
public class ProductMatchRuleRepositoryImpl extends JOOQBaseRepository implements ProductMatchRuleRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return LOAN_PRODUCT_MATCH_RULE;
	}

	@Override
	public Page<ProductMatchRuleSearchResponse> list(ProductMatchRuleSearchRequest searchRequest, Pageable pageable) {
		eq(LOAN_PRODUCT_MATCH_RULE.ADVERT_POSITION, searchRequest.getAdvertPosition());
		eq(LOAN_PRODUCT_MATCH_RULE.RECOMMEND_POSITION, searchRequest.getRecommendPosition());
		return search(pageable, new ProductMatchRuleSearchResponseRecordMapper());
	}

	private final class ProductMatchRuleSearchResponseRecordMapper implements RecordMapper<Record, ProductMatchRuleSearchResponse> {

		@Override
		public ProductMatchRuleSearchResponse map(Record record) {
			ProductMatchRuleSearchResponse response = new ProductMatchRuleSearchResponse();
			response.setId(record.get(LOAN_PRODUCT_MATCH_RULE.ID));
			response.setAdvertPosition(record.get(LOAN_PRODUCT_MATCH_RULE.ADVERT_POSITION));
			response.setRecommendPosition(record.get(LOAN_PRODUCT_MATCH_RULE.RECOMMEND_POSITION));
			response.setCreator(record.get(LOAN_PRODUCT_MATCH_RULE.CREATOR));
			response.setCreateTime(record.get(LOAN_PRODUCT_MATCH_RULE.CREATE_TIME));
			response.setUpdator(record.get(LOAN_PRODUCT_MATCH_RULE.UPDATOR));
			response.setUpdateTime(record.get(LOAN_PRODUCT_MATCH_RULE.UPDATE_TIME));
			return response;
		}
	}
}
